﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.QuoteModules
{
    public class QuoteDetailUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<QuoteDetailModel> GetQuoteDetailUpdater()
        {
            NormalDataUpdater<QuoteDetailModel> systemDataUpdater = new NormalDataUpdater<QuoteDetailModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += ProductionValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<QuoteDetailModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<QuoteDetailModel> e)
        {
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("QuoteId") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv0") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv1") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv2") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv3") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv4") ||
                    e.Insert.PropertyUpdated.Contains("PriceLv5") ||
                    e.Insert.PropertyUpdated.Contains("ProductionId"))
                {
                    e.Message = "已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }
            }
            string message;
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            QuoteModel model = quoteFetcher.Get(e.Insert.Site, e.Insert.QuoteId, out message);
            if (e.Insert.AutoId == 0 && model != null && model.State != Definition.STATE_NORMAL)
            {
                e.Message = "已审核的表单不能新增";
                e.Cancle = true;
                return;
            }
        }
        private void SingletonValid(object sender, DataValidationEventArgs<QuoteDetailModel> e)
        {
            string message;
            QuoteDetailFetcher titleQuoteFetcher = new QuoteDetailFetcher();
            IList<QuoteDetailModel> models = titleQuoteFetcher.ListByMainId(e.Insert.Site, e.Insert.QuoteId, out message, out rows);
            foreach (QuoteDetailModel model in models)
            {
                if (model.AutoId != e.Insert.AutoId && model.ProductionId == e.Insert.ProductionId)
                {
                    e.Message = "本记录已经存在";
                    e.Cancle = true;
                    return;
                }
            }
        }
        private void ProductionValid(object sender,DataValidationEventArgs<QuoteDetailModel> e)
        {
            string message;
            ProductionFetcher fetcher = new ProductionFetcher();
            if(fetcher.Get(e.Insert.Site,e.Insert.ProductionId,out message)==null)
            {
                e.Message = "找不到对应的产品记录";
                e.Cancle = true;
                return;
            }
        }
        private void DataNotSpaceValid(object sender, DataValidationEventArgs<QuoteDetailModel> e)
        {
            if(e.Insert.PriceLv5>e.Insert.PriceLv4
                ||e.Insert.PriceLv4>e.Insert.PriceLv3
                ||e.Insert.PriceLv3>e.Insert.PriceLv2
                ||e.Insert.PriceLv2>e.Insert.PriceLv1
                ||e.Insert.PriceLv1>e.Insert.PriceLv0)
            {
                e.Message = "价格LV0-LV5依次是从高到底";
                e.Cancle = true;
                return;
            }
        }
       
    }
}
